package com.zlm.Stream;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 13026
 */
public class Stream_final_function_demo {
    /*
     * Stream
     *   终结方法
     *      void forEach(Consumer action) 遍历
     *      long count()                  统计
     *      toArray()                     收集流中数据，放到数组中
     *      collect(Collector collector)  收集流中数据，放到集合中
     * */
    public static void main(String[] args) {
        // count
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "1", "2");
        System.out.println(list.stream().count());

        // toArray
        ArrayList<String> list2 = new ArrayList<>();
        Collections.addAll(list2, "1", "2");
        System.out.println(Arrays.toString(list2.stream().toArray()));
        // 下面这两个是一样的，方法引用构造方法
        System.out.println(Arrays.toString(list2.stream().toArray(String[]::new)));
        System.out.println(Arrays.toString(list2.stream().toArray(value -> new String[value])));

        // collect
        ArrayList<String> list3 = new ArrayList<>();
        Collections.addAll(list3, "1", "2");
        // 收集到List
        List<String> collect = list3.stream().collect(Collectors.toList());
        System.out.println(collect);
        // 收集到Set
        Set<String> collect2 = list3.stream().collect(Collectors.toSet());
        System.out.println(collect2);
        // 收集到Map，键不能重复，否则报错
        Map<String, String> collect3 = list3.stream().collect(Collectors.toMap(s -> s, s -> s));
        System.out.println(collect3);
        }
}
